﻿namespace WMS6.Web.UserControls
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class MultipleSelectControl : UserControl
    {
        private DataSet dataSetSource = null;
        private string dataTextField = string.Empty;
        private string dataValueField = string.Empty;
        public DropDownList ddlTempDropDownList = new DropDownList();
        public string DefaultSelected = string.Empty;
        protected ListBox MultipleListBox;
        public DataRow[] sysCode4DDLList = null;
        public string userLanguage = string.Empty;

        public void BindItems()
        {
            List<string> defaultSelectedList = null;
            int i;
            System.Web.UI.WebControls.ListItem item;
            if (!string.IsNullOrEmpty(this.DefaultSelected))
            {
                defaultSelectedList = new List<string>();
                defaultSelectedList = this.DefaultSelected.Split(new char[] { ',' }).ToList<string>();
            }
            if ((this.sysCode4DDLList == null) || (this.sysCode4DDLList.Length <= 0))
            {
                if ((this.ddlTempDropDownList != null) && (this.ddlTempDropDownList.Items.Count > 0))
                {
                    for (i = 0; i < this.ddlTempDropDownList.Items.Count; i++)
                    {
                        this.MultipleListBox.Items.Add(this.ddlTempDropDownList.Items[i]);
                    }
                }
                else if ((this.dataSetSource != null) && (this.dataSetSource.Tables[0].Rows.Count > 0))
                {
                    for (i = 0; this.dataSetSource.Tables[0].Rows.Count > i; i++)
                    {
                        item = new System.Web.UI.WebControls.ListItem(this.dataSetSource.Tables[0].Rows[i][this.dataTextField].ToString(), this.dataSetSource.Tables[0].Rows[i][this.dataValueField].ToString());
                        if ((defaultSelectedList != null) && defaultSelectedList.Contains(item.Value))
                        {
                            item.Selected = true;
                        }
                        this.MultipleListBox.Items.Add(item);
                    }
                }
            }
            else
            {
                string descr = "CN_DESCR";
                string CS40002 = this.userLanguage.ToUpper();
                if (CS40002 != null)
                {
                    if (!(CS40002 == "ZH-CN"))
                    {
                        if (CS40002 == "ZH-TW")
                        {
                            descr = "TW_DESCR";
                        }
                        else if (CS40002 == "EN-*")
                        {
                            descr = "EN_DESCR";
                        }
                    }
                    else
                    {
                        descr = "CN_DESCR";
                    }
                }
                for (i = 0; i < this.sysCode4DDLList.Length; i++)
                {
                    item = new System.Web.UI.WebControls.ListItem(this.sysCode4DDLList[i][descr].ToString(), this.sysCode4DDLList[i]["SYS_CODE"].ToString());
                    if ((defaultSelectedList != null) && defaultSelectedList.Contains(item.Value))
                    {
                        item.Selected = true;
                    }
                    this.MultipleListBox.Items.Add(item);
                }
            }
        }

        private string GetSelectItem()
        {
            string strSelect = "";
            for (int i = 0; i < this.MultipleListBox.Items.Count; i++)
            {
                if (this.MultipleListBox.Items[i].Selected)
                {
                    strSelect = strSelect + this.MultipleListBox.Items[i].Value + ",";
                }
            }
            if (strSelect != "")
            {
                strSelect = strSelect.Substring(0, strSelect.LastIndexOf(","));
            }
            return strSelect;
        }

        private string GetSelectItemText()
        {
            string strSelect = "";
            for (int i = 0; i < this.MultipleListBox.Items.Count; i++)
            {
                if (this.MultipleListBox.Items[i].Selected)
                {
                    strSelect = strSelect + this.MultipleListBox.Items[i].Text + ",";
                }
            }
            if (strSelect != "")
            {
                strSelect = strSelect.Substring(0, strSelect.LastIndexOf(","));
            }
            return strSelect;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.BindItems();
            }
            string checkAll = "全选";
            string cancelAll = "取消";
            string CS40001 = this.userLanguage.ToUpper();
            if (CS40001 != null)
            {
                if (!(CS40001 == "ZH-TW"))
                {
                    if (CS40001 == "EN-*")
                    {
                        checkAll = "All";
                        cancelAll = "Cancel";
                    }
                }
                else
                {
                    checkAll = "全選";
                    cancelAll = "取消";
                }
            }
            ScriptManager.RegisterClientScriptBlock(this, base.GetType(), "ReRegeMultipleSelectControl" + this.MultipleListBox.ClientID, "$(document).ready(function() {  $('#" + this.MultipleListBox.ClientID + "').multiSelect({  noneSelectedText: '', checkAllText: '" + checkAll + "', unCheckAllText: '" + cancelAll + "', selectedList: 100 , minWidth: 150, fadeSpeed: 300,  maxHeight: 180 });}); ", true);
        }

        public string ClientID
        {
            get
            {
                return this.MultipleListBox.ClientID;
            }
        }

        public DataSet DataSetSource
        {
            get
            {
                return this.dataSetSource;
            }
            set
            {
                this.dataSetSource = value;
            }
        }

        public string DataTextField
        {
            get
            {
                return this.dataTextField;
            }
            set
            {
                this.dataTextField = value;
            }
        }

        public string DataValueField
        {
            get
            {
                return this.dataValueField;
            }
            set
            {
                this.dataValueField = value;
            }
        }

        public DropDownList ListItem
        {
            get
            {
                return this.ddlTempDropDownList;
            }
        }

        public DataRow[] ListItemSource
        {
            get
            {
                return this.sysCode4DDLList;
            }
            set
            {
                this.sysCode4DDLList = value;
            }
        }

        public string ReturnText
        {
            get
            {
                return this.GetSelectItemText();
            }
        }

        public string ReturnValue
        {
            get
            {
                return this.GetSelectItem();
            }
        }

        public string UserLanguage
        {
            get
            {
                return this.userLanguage;
            }
            set
            {
                this.userLanguage = value;
            }
        }
    }
}

